import validate from 'wechat-validate'
// pkgUser/pages/reg/reg.js
Page({
  behaviors: [validate],
  /**
   * 页面的初始数据
   */
  data: {
    mobile: '',
    password: '',
    repwd: ''
  },
  rules: {
    mobile: [
      { required: true, message: '请输入手机号' },
      { pattern: /^1[3-8]\d{9}$/, message: '请输入合法的手机号' }
    ],
    password: [
      { required: true, message: '请输入密码' },
      { pattern: /^\d{6,}$/, message: '请输入至少6位的字母、数字组成的密码' }
    ],
  },
  validateRePwd() {
    const flag = this.data.password === this.data.repwd
    !flag && wx.utils.toast('两次输入的密码不一致')
    return flag
  },
  async reg() {
    if (this.validate() && this.validateRePwd()) {
      const res = await wx.http.post('/index/reg', {
        mobile: this.data.mobile,
        password: this.data.password
      })
      if (res.code !== 1000) return wx.utils.toast('注册失败，请重新注册')
      wx.utils.toast('注册成功')
      setTimeout(() => {
        wx.redirectTo({
          url: '/pkgUser/pages/login/login',
        })
      }, 1000)

    }

  }
})